package com.hitgo.soccer.handler;

import java.io.IOException;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.hitgo.soccer.core.Player;
import com.hitgo.soccer.database.PlayerDAO;

public class PlayerHandler extends BaseHandler
{
	private static BaseHandler instance = null;
	
	public static BaseHandler getInstance()
	{
		if(instance == null)
			instance = new PlayerHandler();
		return instance;
	}
	
	@Override
	public void cmdHandler(int nCmd, HttpServletRequest req, HttpServletResponse res)
	{
		String strParams = req.getParameter("fbParams");
		
		byte[] retData = null;
		switch(nCmd)
		{
		case Commands.CMD_PLAYER_TRAIN:
			retData = trainPlayer(res, strParams);
			break;
		}

		res.setContentType("text/plain");
		try
		{
			ServletOutputStream out = res.getOutputStream();
			if(retData == null)
				out.write(0);
			else
				out.write(retData);
		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	private byte[] trainPlayer(HttpServletResponse res, String strParams)
	{
		strParams = strParams.trim();
		String[] params = strParams.split(Commands.PARAMS_SEPARATOR);
		Long lUserID = Long.parseLong(params[0]);
		Integer nPlayerID = Integer.parseInt(params[1]);
		
		Player player= new Player();
		player.setPlayerID(nPlayerID);
		player.setUserID(lUserID);
		
		player = PlayerDAO.getPlayer(player);
		
		return null;
	}
}